#pragma once
#include <iostream>
#include <unordered_map>
#include<stdlib.h>
#include <functional>
#include<unistd.h>
using task_t = std::function<void()>;
static int number = 0;
void DownLoad()
{
    std::cout << "我是下载任务..." << std::endl;
}
void Log()
{
    std::cout << "我是日志任务..." << std::endl;
}
void Sql()
{
    std::cout << "我是数据库同步任务..." << std::endl;
}
class TackManger
{
public:
    TackManger()
    {
        ImsertTask(DownLoad);
        ImsertTask(Log);
        ImsertTask(Sql);
    }
    void ImsertTask(task_t t)
    {
        tasks[number++] = t;
    }
    void Execute(int number)
    {
        if (tasks.find(number) == tasks.end())
            return;
        tasks[number]();
    }
    int SelecTack()
    {
        return rand() % number;
    }
    ~TackManger()
    {
    }

private:
    std::unordered_map<int, task_t> tasks;
};

TackManger tm;

void Worker()
{
    while (true)
    {
        int cmd = 0;
        int n = ::read(0, &cmd, sizeof(cmd));
        if (n == sizeof(cmd))
        {
            tm.Execute(cmd);
        }
        else if(n == 0)
        {
            //std::cout << "pid: " << getpid() << " quit..." << std::endl;
            break;
        }
            
    }
}